<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\Article as articles;
use app\admin\model\ArticleType;
use Db;

class Article extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    //文章列表
    public function index()
    {   
        $list = articles::alias('a')->field('a.*,b.cat_name')->join('article_type b','a.type_id=b.id')->paginate(5);


        return $this->fetch('article_list',['list'=>$list]);
    }
    //文章分类管理
    public function article_type()
    {
        return $this->fetch('article_Sort');
    }
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {   
        //查询出文章的分类
        $list = ArticleType::select();
        return $this->fetch('create',['list'=>$list]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $req)
    {
        $article = new articles;
        $article->title = $req->title;
        $article->fromto = $req->fromto;
        $article->status = $req->status;
        $article->type_id = $req->type_id;
        $article->content = $req->content;
        $article->save();

        return redirect('/admin/article/create')->with('status','1');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {   
        $list = ArticleType::select();
        $data = articles::get($id);
        // var_dump($data);die;
        return $this->fetch('edit',['list'=>$list,'data'=>$data]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function updated(Request $req,$id)
    {           
          $id = (int)$id;

          $article = articles::get($id);
          //var_dump($article);die;
          $article->title = $req->title;
          $article->fromto = $req->fromto;
          $article->status = $req->status;
          $article->type_id = $req->type_id;
          $article->content = $req->content;
          $article->save();
          return redirect('/admin/article/edit/id/'.$id)->with('status','1');
    }

    public function update(Request $request, $status,$id)
    {
          $article = articles::get($id);
          $article->status = $status;
          $article->save();
    }

    //文章分类列表资源显示
    public function type_list(Request $req)
    {   
        if(isset($req->cat_name)&&$req->cat_name!=''){
            $model = new ArticleType;
            $model->cat_name = $req->cat_name;
            $model->save();
        }

        $list = ArticleType::paginate(5);

        return view('type_list',['list'=>$list]);
    }

    //文章分类修改资源显示
    public function type_edit($id)
    {
        $list = ArticleType::get($id);

        return view('type_edit',['list'=>$list]);
    }
    //保存更新的文章分类
    public function type_update(Request $req,$id)
    {
        $model = ArticleType::get($id);
        $model->cat_name = $req->cat_name;
        $model->save();

         return redirect('/admin/article/type_edit/id/'.$id)->with('status','1');
    }
    //删除文章分类
    public function type_delete($id)
    {
        ArticleType::destroy($id);
    }
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        articles::destroy($id);
    }


    //接收发布商品中富文本编辑器中的图片资源
    public function images()
    {   
        $file = request()->file('file');

        $url = env('root_path');
        $info = $file->move( $url.'public/static/uploads/admin/fuwenben/');

        if($info){
            return json([
                'code'=>0,
                'msg'=>'图片上传成功',
                'data'=> [
                    'src'=>'/static/uploads/admin/fuwenben/'.$info->getSaveName(),
                ]
            ]);
        }else{
            return json([
                'code'=>1,
                'msg'=>'图片上传失败',
            ]);
        }
    }
}
